﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

/************************************************************************/
/* CSharp剪贴板监视器
 * Powered by:testcs_dn
 * Blog:http://blog.csdn.net/testcs_dn
 */
/************************************************************************/
namespace CSharp剪贴板监视器
{
    public partial class Form1 : Form
    {
        #region Definitions
        //Constants for API Calls...
        private const int WM_DRAWCLIPBOARD = 0x308;
        private const int WM_CHANGECBCHAIN = 0x30D;

        //Handle for next clipboard viewer...
        private IntPtr mNextClipBoardViewerHWnd;

        //API declarations...
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static public extern bool ChangeClipboardChain(IntPtr HWnd, IntPtr HWndNext);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
        #endregion

        #region Contructor
        public void NewViewer()
        {
            //InitializeComponent()
            //To register this form as a clipboard viewer...
            mNextClipBoardViewerHWnd = SetClipboardViewer(this.Handle);
        }
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void toolStripStatusLabel2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            NewViewer();
            button1.Text = logging ? "记录" : "暂停";
            button2.Text = @"提取数据";
            button3.Text = @"清空";
            button2.Enabled = !logging;
            button3.Enabled = !logging;

            richTextBox1.TextChanged += TextBox1_TextChanged;

            this.TopMost = true;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            this.Text = @"剪贴板监视器 - " + richTextBox1.Lines.Length;
        }

        private bool logging = true;

        #region Message Process
        //Override WndProc to get messages...
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_DRAWCLIPBOARD:
                    {
                        //The clipboard has changed...
                        //##########################################################################
                        // Process Clipboard Here :)........................
                        //##########################################################################
                        SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());

                        //显示剪贴板中的文本信息
                        if (Clipboard.ContainsText())
                        {
                            if (logging)
                            {
                                richTextBox1.AppendText(Clipboard.GetText());

                                richTextBox1.Focus();
                                richTextBox1.SelectionStart = richTextBox1.Text.Length;
                                richTextBox1.ScrollToCaret();

                                this.Text = @"剪贴板监视器 - " + richTextBox1.Lines.Length;
                            }
                        }
                        ////显示剪贴板中的图片信息
                        //if (Clipboard.ContainsImage())
                        //{
                        //    pictureBox1.Image = Clipboard.GetImage();
                        //    pictureBox1.Update();
                        //}
                        break;
                    }
                case WM_CHANGECBCHAIN:
                    {
                        //Another clipboard viewer has removed itself...
                        if (m.WParam == (IntPtr)mNextClipBoardViewerHWnd)
                        {
                            mNextClipBoardViewerHWnd = m.LParam;
                        }
                        else
                        {
                            SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
                        }
                        break;
                    }
            }
            base.WndProc(ref m);
        }
        #endregion

        private void tsmi_about_Click(object sender, EventArgs e)
        {
            openMyBlog();
        }

        private void tsmi_exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsmi_open_Click(object sender, EventArgs e)
        {
            
        }

        private void openMyBlog()
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            logging = !logging;
            button1.Text = logging ? "记录" : "暂停";
            button2.Enabled = !logging;
            button3.Enabled = !logging;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(richTextBox1.Text);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }
    }
}
